//会员管理——会员列表
var starttime = ""; //开始时间
var endtime = ""; //结束时间
var pagesize = 10;
var pageindex = 1;
var sortfild = ""; //排序的关键字
var sortway = ""; //排序：升序、降序
$(function() {

	//下拉框
	$(document).on('click', ".my_select_ul li", function() {
		$(this).parent().siblings("div").find("span").html($(this).text());
		$(this).parent().siblings("div").find("span").attr("value", $(this).attr("value"));

		//获取数据
		$("#first_button").click();

	})

	//排序 
	$(".list_sort_tab").click(function() {
		sortfild = $(this).attr("value");
		if($(this).find("i").hasClass("icon_downs")) {
			$(this).find("i").removeClass("icon_downs");
			$(this).find("i").addClass("icon_ups");
			sortway = "asc";
		} else {
			$(this).find("i").removeClass("icon_ups");
			$(this).find("i").addClass("icon_downs");
			sortway = "desc";
		}
		//必须要，否则排序则不能调用该方法  使用该句，当点击排序，就同时点击了 第一页的按钮，自动调用分页的效果，
		$("#first_button").click();
	});

	//悬浮显示 当宽度小于 文字宽度时，则鼠标移到该内容时，悬浮显示，将文字全部显示出来
	$(document).on("mouseover", ".show_tips", function() {
		if($(this).width() > $(this).parent().width()) {
			layer.tips($(this).html(), $(this), {
				tips: [1, '#3595CC'],
				time: 0
			})
		}
	});
	$(document).on("mouseout", ".show_tips", function() {
		layer.closeAll('tips');
	});

	//全选中或全不选
	$("#checkAll").click(function() {
		if($(this).is(':checked')) {
			$(".checkbox").prop("checked", true);
		} else {
			$(".checkbox").prop("checked", false);
		}
	})

	/*切换搜索条件*/
	$("#conditional_search").find("li").click(function() {
		$("#search_name").text($(this).text());
		$(this).find("a").attr('value');
		$("#search_name").attr('value', $(this).find("a").attr('name'));
	});

	//时间选择
	timesearch();
	var today = new Date();
	startDate = endDate = today.format('yyyy-MM-dd');
	starttime = endtime = startDate;
	$("#date1").html(startDate + " - " + endDate);
	$(".select_time1 li ").click(function() {
		$(".select_time1 li").removeClass("blue_color");
		$(this).addClass("blue_color");
		var time = $(this).attr("name");
		if(time == "0") {
			startDate = endDate = today.format('yyyy-MM-dd');
		} else if(time == "1") {
			startDate = endDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1).format('yyyy-MM-dd');
		} else if(time == "7") {
			startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 6).format('yyyy-MM-dd');
			endDate = today.format('yyyy-MM-dd');
		} else if(time == "30") {
			startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 29).format('yyyy-MM-dd');
			endDate = today.format('yyyy-MM-dd');
		}
		starttime = startDate;
		endtime = endDate;
		$("#date1").html(startDate + " - " + endDate);
		if(time == "") {
			starttime = "";
			endtime = "";
		}
		//获取数据
		$("#first_button").click();
	})

	//图片显示层
	layui.use('layer', function() {
		$ = layui.jquery; //jquery
		layer = layui.layer; //弹出层

		//以上模块根据需要引入
		layer.ready(function() { //为了layer.ext.js加载完毕再执行
			layer.photos({
				photos: '#biuuu_city_list',
				shift: 5 //0-6的选择，指定弹出图片动画类型，默认随机
			});
		});

	});

})

//批量删除提交
function delAll() {
	layer.confirm('确认要删除吗？', function(index) {
		//捉到所有被选中的，发异步进行删除
		layer.msg('删除成功', { icon: 1 });
	});
}

/*添加*/
function question_add(title, url, w, h) {
	x_admin_show(title, url, w, h);
}

//编辑 
function question_edit(title, url, id, w, h) {
	x_admin_show(title, url, w, h);
}

/*删除*/
function question_del(obj, id) {
	layer.confirm('确认要删除吗？', function(index) {
		//发异步删除数据
		$(obj).parents("tr").remove();
		layer.msg('已删除!', { icon: 1, time: 1000 });
	});
}


//时间选择控制器
function timesearch() {

	layui.use('laydate', function() {
		var laydate = layui.laydate;

		//日期范围
		laydate.render({
			elem: '#date1',
			range: true,
			startDate: new Date().format('yyyy-MM-dd'),
			endDate: new Date().format('yyyy-MM-dd'),
			done: function(value, date, endDate) { //选择好日期的回调
				startDate = starttime = value.substring(0, 10);
				endDate = endtime = value.substring(13, 23);
				pageindex = 1;
                $(".select_time1 li").removeClass("blue_color");
				//获取数据
				$("#first_button").click();
			}
		});
	});

}
/**
 * 获取当前时间
 * @returns {String}
 */
function time() {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = parseInt(myDate.getMonth() + 1);
	var day = myDate.getDate();
	var timedate = year + "-" + month + "-" + day;
	return timedate;
};

function time1() {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = parseInt(myDate.getMonth() + 1);
	var day = parseInt(myDate.getDate() - 1);
	var timedate = year + "-" + month + "-" + day;
	return timedate;
};
/**
 * 当前时间段
 */
function timequantum(n) {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = parseInt(myDate.getMonth() + 1);
	var day = myDate.getDate();
	if(n > day) {
		if(month > 1) {
			month = parseInt(month - 1);
		} else {
			year = parseInt(year - 1);
		}
	} else {
		day = parseInt(day - n);
	}
	var timedate = year + "-" + month + "-" + day;
	return timedate;
}

function DateDiff(sDate1, sDate2) { //sDate1和sDate2是yyyy-MM-dd格式
	var aDate, oDate1, oDate2, iDays;
	aDate = sDate1.split("-");
	oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为yyyy-MM-dd格式
	aDate = sDate2.split("-");
	oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
	iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数

	return iDays; //返回相差天数
}
Date.prototype.format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, //month
		"d+": this.getDate(), //day
		"h+": this.getHours(), //hour
		"m+": this.getMinutes(), //minute
		"s+": this.getSeconds(), //second
		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
		"S": this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format = format.replace(RegExp.$1,
		(this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1,
				RegExp.$1.length == 1 ? o[k] :
				("00" + o[k]).substr(("" + o[k]).length));
	return format;
}
